{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基础说明\n",
    "\n",
    "该文档演示一些基础的用法，如打印文字，导入包等。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Hello World\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\r\n"
     ]
    }
   ],
   "source": [
    "Console.WriteLine(\"Hello\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 简单日志\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年10月18日 09:10:26  Debug\n",
      "----Message：【单元测试】调试日志\n",
      "2024年10月18日 09:10:26  Info\n",
      "----Message：【单元测试】信息日志\n",
      "2024年10月18日 09:10:26  Warning\n",
      "----Message：【单元测试】报警日志\n",
      "2024年10月18日 09:10:26  Error\n",
      "----Message：【单元测试】错误日志\n",
      "----异常调用函数: MoveNext\n"
     ]
    }
   ],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\"\n",
    "\n",
    "BaseLogManager.SendLog(LogLevel.Debug, \"【单元测试】调试日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Info, \"【单元测试】信息日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Warning, \"【单元测试】报警日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Error, \"【单元测试】错误日志\", \"\", true);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 保存日志到本地\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年10月18日 09:10:27  Info\n",
      "----Message：删除c:\\Users\\zeros\\Documents\\【项目文件夹】\\【常用】草稿文档\\CSharp\\01_导入外部引用\\SaveLog目录下30天前的数据\n",
      "----删除0个子文件夹，0个子文件\n",
      "2024年10月18日 09:10:27  Debug\n",
      "----Message：【单元测试】调试日志\n",
      "2024年10月18日 09:10:27  Info\n",
      "----Message：【单元测试】信息日志\n",
      "2024年10月18日 09:10:27  Warning\n",
      "----Message：【单元测试】报警日志\n",
      "2024年10月18日 09:10:27  Error\n",
      "----Message：【单元测试】错误日志\n",
      "----异常调用函数: MoveNext\n"
     ]
    }
   ],
   "source": [
    "BaseDefine.DirectoryPathOnSaveLog = \"./SaveLog\";\n",
    "BaseLogManager.Register_SaveLogToFile();\n",
    "\n",
    "BaseLogManager.SendLog(LogLevel.Debug, \"【单元测试】调试日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Info, \"【单元测试】信息日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Warning, \"【单元测试】报警日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Error, \"【单元测试】错误日志\", \"\", true);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 保存日志到数据库\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年10月18日 09:10:27  Debug\n",
      "----Message：【单元测试】调试日志\n",
      "2024年10月18日 09:10:28  Info\n",
      "----Message：【单元测试】信息日志\n",
      "2024年10月18日 09:10:28  Warning\n",
      "----Message：【单元测试】报警日志\n",
      "2024年10月18日 09:10:28  Error\n",
      "----Message：【单元测试】错误日志\n",
      "----异常调用函数: MoveNext\n"
     ]
    }
   ],
   "source": [
    "BaseLogManager.Register_SaveLogToSQL(\"192.168.2.120\", \"root\", \"123456\");\n",
    "\n",
    "BaseLogManager.SendLog(LogLevel.Debug, \"【单元测试】调试日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Info, \"【单元测试】信息日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Warning, \"【单元测试】报警日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Error, \"【单元测试】错误日志\", \"\", true);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 注册日志输出函数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: 【单元测试】调试日志\n",
      "1: 【单元测试】信息日志\n",
      "2: 【单元测试】报警日志\n",
      "3: 【单元测试】错误日志\n"
     ]
    }
   ],
   "source": [
    "void LogFunction(Log _log) => Console.WriteLine($\"{_index++}: {_log.LogMessage}\");\n",
    "\n",
    "int _index = 0;\n",
    "BaseLogManager.Clear_SendLogFunction();\n",
    "BaseLogManager.Register_SendLogFunction(LogFunction);\n",
    "\n",
    "BaseLogManager.SendLog(LogLevel.Debug, \"【单元测试】调试日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Info, \"【单元测试】信息日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Warning, \"【单元测试】报警日志\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Error, \"【单元测试】错误日志\", \"\", true);\n",
    "\n",
    "BaseLogManager.Clear_SendLogFunction();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数组格式化为 16 进制的字符串\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01 23 45 67 89 AB CD EF\r\n"
     ]
    }
   ],
   "source": [
    "byte[] _byte_array = [1, 35, 69, 103, 137, 171, 205, 239];\n",
    "string _string = BaseTransform.ByteToHexStr(_byte_array);\n",
    "_string.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 16 进制字符串转换为字节数组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 35, 69, 103, 137, 171, 205, 14\r\n"
     ]
    }
   ],
   "source": [
    "string _string = \"01 23 45 67 89 AB CD E\";\n",
    "byte[] _byte_array = BaseTransform.HexStrToByte(_string);\n",
    "_byte_array.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 16 进制字符串格式化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01 23 45 67 89 AB CD EF\r\n"
     ]
    }
   ],
   "source": [
    "string _string = \"0123456789ABCDEF\";\n",
    "string _string_formate = BaseTransform.HexStringFormatting(_string);\n",
    "_string_formate.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数组格式化显示【byte 数组】\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|  DEC |  HEX |       Bin |\r\n",
      "| ---- | ---- | --------- |\r\n",
      "|    1 |   01 | 0000-0001 |\r\n",
      "|   35 |   23 | 0010-0011 |\r\n",
      "|   69 |   45 | 0100-0101 |\r\n",
      "|  103 |   67 | 0110-0111 |\r\n",
      "|  137 |   89 | 1000-1001 |\r\n",
      "|  171 |   AB | 1010-1011 |\r\n",
      "|  205 |   CD | 1100-1101 |\r\n",
      "|  239 |   EF | 1110-1111 |\r\n",
      "|    0 |   00 | 0000-0000 |\r\n",
      "|  255 |   FF | 1111-1111 |\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "byte[] _bytes = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, byte.MinValue, byte.MaxValue];\n",
    "BaseTransform.FormatShowArray(_bytes).Dump();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "uint[] _uints = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, uint.MinValue, uint.MaxValue];\n",
    "BaseTransform.FormatShowArray(_uints);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### `BaseSocketClient` 测试用例\n",
    "\n",
    "该测试用例会在本地创建一个 `Socket` 服务器，使用 `5001` 端口号，然后使用 `BaseSocketClient` 对象进行连接。\n",
    "\n",
    "测试如下功能：\n",
    "\n",
    "1. 连接 Socket 服务器\n",
    "2. 发送消息\n",
    "3. 发送消息等待回复 （耗时 2 秒）\n",
    "4. 发送消息等待回复超时（耗时 5 秒）\n",
    "5. 发送消息等待回复取消 （耗时 2 秒）\n",
    "6. 发送消息等待回复 （耗时 1 秒）\n",
    "\n",
    "`BaseSocketClient` 提供的发送消息并等待功能根据末尾的字符串决定接收回复完成。在该测试用例中，只有接收到的字符串末尾为 `A` 时，`SendMessage_WaitReply` 才会返回。否则会持续等待，直到超时或者取消。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年10月18日 09:10:31  Debug\n",
      "----Message：发送Hello 0，等待末尾为A的字符串回复，理论耗时2秒，实际耗时：2166ms\n",
      "2024年10月18日 09:10:36  Error\n",
      "----Message：【客户端】发送字符串消息【Hello 1】错误：接收回复超时\n",
      "----System.Net.Sockets.SocketException (10060): 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。\n",
      "----   at System.Net.Sockets.Socket.Receive(Byte[] buffer)\n",
      "----   at BaseTool.BaseSocketClient.SendMessage_WaitReply(String _message, String _end_string, Int32 _milliseconds) in C:\\Users\\zeros\\Documents\\VisualStudioCode\\ToolsRepository V4.0\\BaseTool\\BaseSocketClient.cs:line 277\n",
      "----异常调用函数: SendMessage_WaitReply\n",
      "2024年10月18日 09:10:36  Debug\n",
      "----Message：发送Hello 1，等待末尾为A的字符串回复超时，理论耗时5秒，实际耗时：5230ms\n",
      "2024年10月18日 09:10:38  Error\n",
      "----Message：【客户端】发送字符串消息【Hello 2】错误：取消等待回复\n",
      "----System.Net.Sockets.SocketException (10053): 你的主机中的软件中止了一个已建立的连接。\n",
      "----   at System.Net.Sockets.Socket.Receive(Byte[] buffer)\n",
      "----   at BaseTool.BaseSocketClient.SendMessage_WaitReply(String _message, String _end_string, Int32 _milliseconds) in C:\\Users\\zeros\\Documents\\VisualStudioCode\\ToolsRepository V4.0\\BaseTool\\BaseSocketClient.cs:line 277\n",
      "----异常调用函数: SendMessage_WaitReply\n",
      "2024年10月18日 09:10:39  Debug\n",
      "----Message：发送Hello 2，等待操作取消，理论耗时2秒，实际耗时：2204ms\n",
      "2024年10月18日 09:10:40  Debug\n",
      "----Message：发送Hello 3，等待末尾为A的字符串回复，理论耗时1秒，实际耗时：1045ms\n"
     ]
    }
   ],
   "source": [
    "using System.Diagnostics;\n",
    "using System.Net;\n",
    "using System.Net.Sockets;\n",
    "\n",
    "Socket _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n",
    "_listener.Bind(IPEndPoint.Parse(\"127.0.0.1:5001\"));\n",
    "_listener.Listen();\n",
    "\n",
    "BaseSocketClient _client = new BaseSocketClient(\"127.0.0.1\", 5001, \"客户端\");\n",
    "\n",
    "Task _task1 = Task.Run(() =>\n",
    "{\n",
    "    _client.Connect();\n",
    "\n",
    "    // 等待1秒后发送前两个字节，再等1秒，发送后两个字节\n",
    "    Stopwatch _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply(\"Hello 0\", \"A\");\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"发送Hello 0，等待末尾为A的字符串回复，理论耗时2秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "    // 操作超时\n",
    "    _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply(\"Hello 1\", \"A\");\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"发送Hello 1，等待末尾为A的字符串回复超时，理论耗时5秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "    // 操作取消\n",
    "    _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply(\"Hello 2\", \"A\");\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"发送Hello 2，等待操作取消，理论耗时2秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "    _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply(\"Hello 3\", \"A\");\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"发送Hello 3，等待末尾为A的字符串回复，理论耗时1秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "});\n",
    "\n",
    "Socket _server;\n",
    "Task _task2 = Task.Run(() =>\n",
    "{\n",
    "    _server = _listener.Accept();\n",
    "\n",
    "    Task.Delay(1000).Wait();\n",
    "    _server.Send(Encoding.Default.GetBytes(\"Reply 1\"));\n",
    "    Task.Delay(1000).Wait();\n",
    "    _server.Send(Encoding.Default.GetBytes(\"A\"));\n",
    "\n",
    "    Task.Delay(5000).Wait();\n",
    "\n",
    "    _server = _listener.Accept();\n",
    "    Task.Delay(2000).Wait();\n",
    "    _client.CancelWait();\n",
    "\n",
    "    _server = _listener.Accept();\n",
    "    Task.Delay(1000).Wait();\n",
    "    _server.Send(Encoding.Default.GetBytes(\"Reply 1 A\"));\n",
    "});\n",
    "\n",
    "Task.WhenAll(_task1, _task2).Wait();\n",
    "_client.DisConnect();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`BaseSocketClient` 也支持发送字节数组。\n",
    "\n",
    "在等待字节数组的回复的时候，根据字节数组长度判断接收回复是否完成。该测试用例中，回复的字节数组长度必须为 4，否则会持续等待。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年10月18日 09:10:42  Debug\n",
      "----Message：等待长度为4的字节数组理论耗时2秒，实际耗时：2005ms\n",
      "2024年10月18日 09:10:47  Error\n",
      "----Message：【客户端】发送字节消息【01】错误：接收回复超时\n",
      "----System.Net.Sockets.SocketException (10060): 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。\n",
      "----   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\n",
      "----   at BaseTool.BaseSocketClient.SendMessage_WaitReply(Byte[] _data, Int32 _reply_length, Int32 _milliseconds) in C:\\Users\\zeros\\Documents\\VisualStudioCode\\ToolsRepository V4.0\\BaseTool\\BaseSocketClient.cs:line 201\n",
      "----异常调用函数: SendMessage_WaitReply\n",
      "2024年10月18日 09:10:47  Debug\n",
      "----Message：等待长度为4的字节数组超时，理论耗时5秒，实际耗时：5166ms\n",
      "2024年10月18日 09:10:50  Error\n",
      "----Message：【客户端】发送字节消息【02】错误：取消等待回复\n",
      "----System.Net.Sockets.SocketException (10053): 你的主机中的软件中止了一个已建立的连接。\n",
      "----   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\n",
      "----   at BaseTool.BaseSocketClient.SendMessage_WaitReply(Byte[] _data, Int32 _reply_length, Int32 _milliseconds) in C:\\Users\\zeros\\Documents\\VisualStudioCode\\ToolsRepository V4.0\\BaseTool\\BaseSocketClient.cs:line 201\n",
      "----异常调用函数: SendMessage_WaitReply\n",
      "2024年10月18日 09:10:50  Debug\n",
      "----Message：操作取消，理论耗时2秒，实际耗时：2247ms\n",
      "2024年10月18日 09:10:51  Debug\n",
      "----Message：等待长度为4的字节数组理论耗时1秒，实际耗时：1073ms\n"
     ]
    }
   ],
   "source": [
    "Socket _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n",
    "_listener.Bind(IPEndPoint.Parse(\"127.0.0.1:5000\"));\n",
    "_listener.Listen();\n",
    "\n",
    "BaseSocketClient _client = new BaseSocketClient(\"127.0.0.1\", 5000, \"客户端\");\n",
    "\n",
    "Task _task1 = Task.Run(() =>\n",
    "{\n",
    "    _client.Connect();\n",
    "\n",
    "    // 等待1秒后发送前两个字节，再等1秒，发送后两个字节\n",
    "    Stopwatch _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply([0x00], 4);\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"等待长度为4的字节数组理论耗时2秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "    // 操作超时\n",
    "    _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply([0x01], 4);\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"等待长度为4的字节数组超时，理论耗时5秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "    // 操作取消\n",
    "    _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply([0x02], 4);\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"操作取消，理论耗时2秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "    _watcher = Stopwatch.StartNew();\n",
    "    _client.SendMessage_WaitReply([0x00], 4);\n",
    "    BaseLogManager.SendLog(LogLevel.Debug, $\"等待长度为4的字节数组理论耗时1秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "});\n",
    "\n",
    "Socket _server;\n",
    "Task _task2 = Task.Run(() =>\n",
    "{\n",
    "    _server = _listener.Accept();\n",
    "\n",
    "    Task.Delay(1000).Wait();\n",
    "    _server.Send(new byte[] { 0x01, 0x02 });\n",
    "    Task.Delay(1000).Wait();\n",
    "    _server.Send(new byte[] { 0x03, 0x04 });\n",
    "\n",
    "    Task.Delay(5000).Wait();\n",
    "\n",
    "    _server = _listener.Accept();\n",
    "    Task.Delay(2000).Wait();\n",
    "    _client.CancelWait();\n",
    "\n",
    "    _server = _listener.Accept();\n",
    "    Task.Delay(1000).Wait();\n",
    "    _server.Send(new byte[] { 0x01, 0x02, 0x03, 0x04 });\n",
    "});\n",
    "\n",
    "Task.WhenAll(_task1, _task2).Wait();\n",
    "_client.DisConnect();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### BaseSocketServer 测试用例\n",
    "\n",
    "该类暂时还没完成。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年10月18日 09:10:51  Debug\n",
      "----Message：【服务器】开启监听...\n",
      "2024年10月18日 09:10:52  Debug\n",
      "----Message：【服务器】接收远程地址【127.0.0.1】连接...\n",
      "2024年10月18日 09:10:52  Debug\n",
      "----Message：【服务器】接收字节消息【00】至远程地址【127.0.0.1】\n",
      "2024年10月18日 09:10:52  Debug\n",
      "----Message：正常接收数据，实际耗时：283ms\n",
      "2024年10月18日 09:10:52  Debug\n",
      "----Message：---------------------------------------------\n",
      "2024年10月18日 09:10:52  Debug\n",
      "----Message：【服务器】远程地址【127.0.0.1】断开连接，重新开启监听\n",
      "2024年10月18日 09:10:53  Debug\n",
      "----Message：【服务器】接收远程地址【127.0.0.1】连接...\n",
      "2024年10月18日 09:10:53  Debug\n",
      "----Message：---------------------------------------------\n",
      "2024年10月18日 09:10:53  Debug\n",
      "----Message：【服务器】接收字节消息【01】至远程地址【127.0.0.1】\n",
      "2024年10月18日 09:10:53  Debug\n",
      "----Message：重连后接收数据，实际耗时：46ms\n",
      "2024年10月18日 09:10:53  Debug\n",
      "----Message：---------------------------------------------\n",
      "2024年10月18日 09:10:53  Debug\n",
      "----Message：【服务器】接收字节消息【03】至远程地址【127.0.0.1】\n",
      "2024年10月18日 09:10:54  Debug\n",
      "----Message：【服务器】远程地址【127.0.0.1】断开连接，重新开启监听\n",
      "2024年10月18日 09:10:54  Error\n",
      "----Message：【客户端】发送字节消息【03】错误：取消等待回复\n",
      "----System.Net.Sockets.SocketException (10053): 你的主机中的软件中止了一个已建立的连接。\n",
      "----   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\n",
      "----   at BaseTool.BaseSocketClient.SendMessage_WaitReply(Byte[] _data, Int32 _reply_length, Int32 _milliseconds) in C:\\Users\\zeros\\Documents\\VisualStudioCode\\ToolsRepository V4.0\\BaseTool\\BaseSocketClient.cs:line 201\n",
      "----异常调用函数: SendMessage_WaitReply\n",
      "2024年10月18日 09:10:54  Debug\n",
      "----Message：操作取消，理论耗时1秒，实际耗时：1105ms\n",
      "2024年10月18日 09:10:55  Debug\n",
      "----Message：---------------------------------------------\n",
      "2024年10月18日 09:10:55  Debug\n",
      "----Message：【服务器】接收远程地址【127.0.0.1】连接...\n",
      "2024年10月18日 09:10:55  Debug\n",
      "----Message：【服务器】接收字节消息【04】至远程地址【127.0.0.1】\n",
      "2024年10月18日 09:11:00  Error\n",
      "----Message：【客户端】发送字节消息【04】错误：接收回复超时\n",
      "----System.Net.Sockets.SocketException (10060): 由于连接方在一段时间后没有正确答复或连接的主机没有反应，连接尝试失败。\n",
      "----   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\n",
      "----   at BaseTool.BaseSocketClient.SendMessage_WaitReply(Byte[] _data, Int32 _reply_length, Int32 _milliseconds) in C:\\Users\\zeros\\Documents\\VisualStudioCode\\ToolsRepository V4.0\\BaseTool\\BaseSocketClient.cs:line 201\n",
      "----异常调用函数: SendMessage_WaitReply\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：【服务器】远程地址【127.0.0.1】断开连接，重新开启监听\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：操作超时，理论耗时5秒，实际耗时：5357ms\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：---------------------------------------------\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：【服务器】断开远程地址【127.0.0.1】连接...\n",
      "2024年10月18日 09:11:00  Warning\n",
      "----Message：【服务器】结束监听\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：客户端主动断开\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：---------------------------------------------\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：【服务器】开启监听...\n",
      "2024年10月18日 09:11:00  Debug\n",
      "----Message：【服务器】接收远程地址【127.0.0.1】连接...\n",
      "2024年10月18日 09:11:01  Debug\n",
      "----Message：【服务器】断开远程地址【127.0.0.1】连接...\n",
      "2024年10月18日 09:11:01  Warning\n",
      "----Message：【服务器】结束数据接收\n",
      "2024年10月18日 09:11:01  Debug\n",
      "----Message：服务器主动断开\n",
      "2024年10月18日 09:11:01  Debug\n",
      "----Message：---------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "BaseSocketServer _server = new BaseSocketServer(6666, \"服务器\");\n",
    "_server.Listen();\n",
    "Task.Delay(100).Wait();\n",
    "\n",
    "BaseSocketClient _client = new BaseSocketClient(\"127.0.0.1\", 6666, \"客户端\");\n",
    "_client.Connect();\n",
    "\n",
    "Stopwatch _watcher = Stopwatch.StartNew();\n",
    "_client.SendMessage([00]);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"正常接收数据，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);\n",
    "Task.Delay(100).Wait();\n",
    "_client.DisConnect();\n",
    "Task.Delay(100).Wait();\n",
    "_client.Connect();\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);\n",
    "\n",
    "_watcher = Stopwatch.StartNew();\n",
    "_client.SendMessage([01]);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"重连后接收数据，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);\n",
    "\n",
    "_watcher = Stopwatch.StartNew();\n",
    "Task.Run(() => { _client.SendMessage_WaitReply([03], 1); });\n",
    "Task.Delay(1000).Wait();\n",
    "_client.CancelWait();\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"操作取消，理论耗时1秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);\n",
    "\n",
    "_watcher = Stopwatch.StartNew();\n",
    "_client.SendMessage_WaitReply([04], 1);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"操作超时，理论耗时5秒，实际耗时：{_watcher.ElapsedMilliseconds}ms\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);\n",
    "\n",
    "_client.DisConnect();\n",
    "Task.Delay(100).Wait();\n",
    "_server.Close();\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"客户端主动断开\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);\n",
    "\n",
    "_server.Listen();\n",
    "_client.Connect();\n",
    "Task.Delay(100).Wait();\n",
    "_server.Close();\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"服务器主动断开\", \"\", true);\n",
    "BaseLogManager.SendLog(LogLevel.Debug, $\"---------------------------------------------\", \"\", true);"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
